package com.sevenwonders;

import java.util.ArrayList;
import java.util.List;

import com.sevenwonders.core.City;
import com.sevenwonders.core.City.Side;
import com.sevenwonders.core.Game;
import com.sevenwonders.core.Player;
import com.sevenwonders.util.Helper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;

public class Dialogs {

    private Dialogs() {
        throw new AssertionError();
    }

    public static void createNewGameDialog(final Context context) {
        try {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View view = inflater.inflate(R.layout.dialog_newgame, null);

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setView(view);
            final AlertDialog newGameDialog = builder.create();

            Button startButton = (Button) view.findViewById(R.id.button_start);
            startButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    try {
                        String playerName = Helper.getPreference(v.getContext(), SettingsActivity.KEY_NAME, "You");
                        int[] spinnerIds = new int[] { R.id.spinner_opponent1, R.id.spinner_opponent2, R.id.spinner_opponent3, R.id.spinner_opponent4,
                            R.id.spinner_opponent5, R.id.spinner_opponent6 };
                        List<String> opponents = new ArrayList<String>();

                        for (int id : spinnerIds) {
                            Spinner opponentSpinner = (Spinner) view.findViewById(id);
                            String opponentString = (String) opponentSpinner.getSelectedItem();

                            if (!"None".equals(opponentString)) {
                                opponents.add(opponentString);
                            }
                        }

                        Game game = new Game();
                        game.setup(playerName, opponents);
                        newGameDialog.dismiss();

                        boolean chooseSide = Helper.getBooleanPreference(v.getContext(), SettingsActivity.KEY_CHOOSE_SIDE, true);
                        if (chooseSide) {
                            createChooseSideDialog(context);
                        } else {
                            Player p = game.getPlayers()
                                .get(0);
                            p.getCity()
                                .setSide(Game.rand.nextBoolean() ? Side.A : Side.B);
                            startGame(v.getContext());
                        }
                    } catch (Throwable t) {
                        String stackTrace = Log.getStackTraceString(t);
                        Log.e("GameActivity", stackTrace, t);
                    }
                }
            });

            Button cancelButton = (Button) view.findViewById(R.id.button_cancel);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    newGameDialog.dismiss();
                }
            });

            newGameDialog.show();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static void createAboutDialog(Context context) {
        try {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_about, null);

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setView(view);
            AlertDialog aboutDialog = builder.create();
            aboutDialog.show();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static void createChooseSideDialog(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = inflater.inflate(R.layout.dialog_chooseside, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        AlertDialog chooseSideDialog = builder.create();

        setupCity(view.findViewById(R.id.layout_city_a), Side.A, chooseSideDialog, inflater);
        setupCity(view.findViewById(R.id.layout_city_b), Side.B, chooseSideDialog, inflater);

        chooseSideDialog.show();
    }

    private static void setupCity(View cityView, final Side side, final AlertDialog chooseSideDialog, LayoutInflater inflater) {
        final City city = Game.myGame.getPlayers()
            .get(0)
            .getCity();

        cityView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View cityView) {
                city.setSide(side);
                chooseSideDialog.dismiss();
                startGame(cityView.getContext());
            }
        });

        UI.setupCity(cityView, city, side, inflater);
    }

    private static void startGame(Context context) {
        ((Activity) context).startActivity(new Intent(context, GameActivity.class));
    }
}
